﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class DictionaryExtension
{
    public static void TryAdd<TKey,TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value)
    {
        if(dic.ContainsKey(key))
        {
            dic[key] = value;
        }
        else
        {
            dic.Add(key,value);
        }
    }

    public static T2 GetValueByIndex<T1, T2>(this Dictionary<T1, T2> dic, int index)
    {
        if (index > dic.Count || index < 0) { return default; }
        int record = 0;
        T1 temp = default;
        foreach (var item in dic)
        {
            if (record == index)
            {
                temp = item.Key;
                return dic[temp];
            }
            record += 1;
        }
        return default;
    }

    public static T1 GetKeyByIndex<T1, T2>(this Dictionary<T1, T2> dic, int index)
    {
        if (index > dic.Count || index < 0) { return default; }
        int record = 0;
      
        foreach (var item in dic)
        {
            if (record == index)
            {
                return item.Key;
            }
            record += 1;
        }
        return default;
    }

}
